<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .container {
        width: 200px;
        height: 200px;
        background: red;
      }

      .box {
        width: 100px;
        height: 100px;
        background: blue;
      }
    </style>
  </head>

  <body>
    <div class="container">
      <div class="box"></div>
    </div>
    <script>
      //一、 阻止事件冒泡// stopPropagation ：停止传播；
      // cancelBubble= true;阻止冒泡
      var containerEle = document.querySelector(".container");
      containerEle.onclick = function () {
        this.style.background = "pink";
      };

      var boxEle = document.querySelector(".box");
      var flag = 0;
      boxEle.onclick = function (e) {
        // 方式一：
        e.stopPropagation();
        // 方式二
        // e.cancelBubble = true;
        if (flag % 2 == 0) {
          this.style.background = "black";
        } else this.style.background = "blue";
        flag++;
      };
    </script>
  </body>
</html>
